﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.ExportToPLMX.SimulationStateExporter.CApExportToPlmxStateExportHandler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using EngineeringInternalExtension.ObjectsStateExportService;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands.ExportToPLMX.SimulationStateExporter;

internal class CApExportToPlmxStateExportHandler : TxStateExporterHandlerBaseEx
{
  private CApExportToPlmxStateModificationExporter m_modificationExporter;
  private string m_simulationExportPath;
  private TxSnapshot m_preSimulationSnapshot;
  private TxObjectList m_indexObjectList = new TxObjectList();
  private ITxOperation m_operationToRestore;
  private TxSnapshot m_startSimulationSnapshot;

  internal Dictionary<ITxObject, int> ObjectToIndexMap { get; private set; } = new Dictionary<ITxObject, int>();

  internal ITxOperation SimulatedOperation { private get; set; }

  internal CApExportToPlmxSimulationFileExporter FileExporter { get; set; }

  public CApExportToPlmxStateExportHandler(string simulationExportPath, List<string> objectIds)
  {
    this.m_simulationExportPath = simulationExportPath;
    this.ObjectHierarchy = new TxStateObjectsHierarchyEx(objectIds, (TxObjectList) null);
  }

  public void ExportSimulation(ITxOperation operation)
  {
    this.SimulatedOperation = operation;
    this.StateExporterManager.Initialize();
    try
    {
      this.StateExporterManager.SimulationPlayer.PlayWithoutRefresh();
    }
    catch (Exception ex)
    {
      string message = ex.Message;
    }
    this.ResetSimulationState();
    this.SetEndState();
    this.FileExporter.SaveToFile(this.m_simulationExportPath);
    this.StateExporterManager.Uninitialize();
  }

  public virtual ITxStateModificationExporterEx ModificationExporter
  {
    get => (ITxStateModificationExporterEx) this.m_modificationExporter;
  }

  public virtual void OnTimeIntervalReached(double time)
  {
    if (this.FileExporter == null)
      return;
    if (TxSnapshot.op_Equality((TxSnapshot) null, (ITxObject) this.m_startSimulationSnapshot))
    {
      this.m_startSimulationSnapshot = this.CreateSnapshot("PlmxStartSimRestoreSnapshot");
      this.FileExporter.animOperations.Add(new CApExportToPlmxObjectsFrameData());
    }
    this.m_modificationExporter = new CApExportToPlmxStateModificationExporter(time, this.ObjectToIndexMap);
    this.ExportModification();
    this.FileExporter.animOperations.Add(this.m_modificationExporter.ObjectsFrameData);
    this.m_modificationExporter = (CApExportToPlmxStateModificationExporter) null;
  }

  public virtual void OnOperationStartedForward(ITxOperation operation, double time)
  {
    if (this.FileExporter == null && operation == this.SimulatedOperation)
      this.SetInitialState();
    if (this.FileExporter == null || ((ITxObject) operation).Collection != this.SimulatedOperation)
      return;
    this.RecordWorkInstructionStep(operation, time);
  }

  public virtual void OnOperationEndedForward(ITxOperation operation, double time)
  {
    CApExportToPlmxSimulationFileExporter fileExporter = this.FileExporter;
  }

  public virtual void InternalInit()
  {
    base.InternalInit();
    this.CreateInitialSnapshot();
    this.AssignIndexToObjects();
    this.InitializeOperation();
  }

  private void InitializeOperation()
  {
    this.m_operationToRestore = TxApplication.ActiveDocument.CurrentOperation;
    TxApplication.ActiveDocument.CurrentOperation = (ITxOperation) null;
    TxApplication.ActiveDocument.CurrentOperation = this.SimulatedOperation;
  }

  public virtual void InternalUnInit()
  {
    base.InternalUnInit();
    if (this.StateExporterManager.SimulationPlayer != null)
    {
      this.StateExporterManager.SimulationPlayer.Rewind();
      TxApplication.ActiveDocument.CurrentOperation = (ITxOperation) null;
      TxApplication.ActiveDocument.CurrentOperation = this.m_operationToRestore;
    }
    if (TxSnapshot.op_Inequality((TxSnapshot) null, (ITxObject) this.m_preSimulationSnapshot))
    {
      this.ApplySnapshot(this.m_preSimulationSnapshot);
      this.m_preSimulationSnapshot.Delete();
      this.m_preSimulationSnapshot = (TxSnapshot) null;
    }
    if (TxSnapshot.op_Inequality((TxSnapshot) null, (ITxObject) this.m_startSimulationSnapshot))
    {
      this.m_startSimulationSnapshot.Delete();
      this.m_startSimulationSnapshot = (TxSnapshot) null;
    }
    this.FileExporter = (CApExportToPlmxSimulationFileExporter) null;
  }

  public void ResetSimulationState()
  {
    if (!TxSnapshot.op_Inequality((TxSnapshot) null, (ITxObject) this.m_startSimulationSnapshot))
      return;
    this.ApplySnapshot(this.m_startSimulationSnapshot);
  }

  private void SetInitialState() => this.FileExporter = new CApExportToPlmxSimulationFileExporter();

  private void SetEndState()
  {
    int count = this.ObjectToIndexMap.Count;
    ITxObject[] itxObjectArray = new ITxObject[count];
    foreach (ITxObject key in this.ObjectToIndexMap.Keys)
    {
      int objectToIndex = this.ObjectToIndexMap[key];
      itxObjectArray[objectToIndex - 1] = key;
    }
    string[] strArray = new string[count];
    for (int index = 0; index < count; ++index)
    {
      ITxObject itxObject = itxObjectArray[index];
      strArray[index] = itxObjectArray[index].Id;
      this.StateExporterManager.SetAllModificationsOnObject(itxObject);
    }
    this.StateExporterManager.SetCameraModified();
    this.m_modificationExporter = new CApExportToPlmxStateModificationExporter(0.0, this.ObjectToIndexMap);
    this.ExportModification();
    if (this.FileExporter.animOperations.Count > 0)
      this.FileExporter.animOperations[0] = this.m_modificationExporter.ObjectsFrameData;
    this.m_modificationExporter = (CApExportToPlmxStateModificationExporter) null;
    this.FileExporter.idArray = strArray;
  }

  private void CreateInitialSnapshot()
  {
    this.m_preSimulationSnapshot = this.CreateSnapshot("PlmxRestoreSnapshot");
  }

  private void AssignIndexToObjects()
  {
    int num1 = 0;
    TxObjectList txObjectList = this.ObjectHierarchy.ObjectsByLevel(num1);
    int num2 = 1;
    for (; txObjectList != null; txObjectList = this.ObjectHierarchy.ObjectsByLevel(num1))
    {
      foreach (ITxObject key in (Collection<ITxObject>) txObjectList)
      {
        this.ObjectToIndexMap.Add(key, num2);
        ((Collection<ITxObject>) this.m_indexObjectList).Add(key);
        ++num2;
      }
      ++num1;
    }
  }

  private void ApplySnapshot(TxSnapshot snapshot)
  {
    snapshot.Apply(new TxApplySnapshotParams()
    {
      ObjectsLocation = true,
      ObjectsVisibility = true,
      PointOfView = true,
      DevicePoses = true,
      ObjectsAttachments = true,
      ObjectsColor = true,
      ObjectsViewingMode = true,
      PMIsScale = true
    });
  }

  private TxSnapshot CreateSnapshot(string snapshotName)
  {
    return TxApplication.ActiveDocument.PhysicalRoot.CreateSnapshot(new TxSnapshotCreationData()
    {
      Name = snapshotName
    });
  }

  private void RecordWorkInstructionStep(ITxOperation operation, double opTime)
  {
    this.FileExporter.animSteps.Add(new CApExportToPlmxOperationsData()
    {
      time = opTime,
      description = operation.Description,
      name = ((ITxObject) operation).Name
    });
  }
}
